class AlterPageController {
	def alterPageService
	
    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        [alterPageInstanceList: AlterPage.list(params), alterPageInstanceTotal: AlterPage.count()]
    }

    def create = {
        def alterPageInstance = new AlterPage()
        alterPageInstance.properties = params
        return [alterPageInstance: alterPageInstance]
    }

    def save = {
		def result = alterPageService.add(request.getFile('file'), params, session.user)
		if(!result.instance.hasErrors()){
			flash.message = "alterPage.created"
            flash.args = [result.instance.id]
            flash.defaultMessage = "AlterPage ${result.instance.id} created"
            redirect(action: "show", id: result.instance.id)
    	}
		else {
			render(view: "create", model: [alterPageInstance: result.instance])
		}
    	return
    }

    def show = {
        def alterPageInstance = AlterPage.get(params.id)
        if (!alterPageInstance) {
            flash.message = "alterPage.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "AlterPage not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [alterPageInstance: alterPageInstance]
        }
    }

    def edit = {
        def alterPageInstance = AlterPage.get(params.id)
        if (!alterPageInstance) {
            flash.message = "alterPage.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "AlterPage not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [alterPageInstance: alterPageInstance]
        }
    }

    def update = {
        def alterPageInstance = AlterPage.get(params.id)
        if (alterPageInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (alterPageInstance.version > version) {
                    
                    alterPageInstance.errors.rejectValue("version", "alterPage.optimistic.locking.failure", "Another user has updated this AlterPage while you were editing")
                    render(view: "edit", model: [alterPageInstance: alterPageInstance])
                    return
                }
            }
            alterPageInstance.properties = params
            if (!alterPageInstance.hasErrors() && alterPageInstance.save()) {
                flash.message = "alterPage.updated"
                flash.args = [params.id]
                flash.defaultMessage = "AlterPage ${params.id} updated"
                redirect(action: "show", id: alterPageInstance.id)
            }
            else {
                render(view: "edit", model: [alterPageInstance: alterPageInstance])
            }
        }
        else {
            flash.message = "alterPage.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "AlterPage not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def alterPageInstance = AlterPage.get(params.id)
        if (alterPageInstance) {
            try {
                alterPageInstance.delete()
                flash.message = "alterPage.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "AlterPage ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "alterPage.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "AlterPage ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "alterPage.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "AlterPage not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
